﻿package {
	
	public class RegionSet {
		
		private var regions:Array;
		
		public function RegionSet() {
			init();
		}
		
		public function init() {
			regions = new Array();
		}
		
		public function setup(rows:int,columns:int, roomW:int, roomH:int) {
			if (rows<1) rows=1;
			if (columns<1) columns=1;
			
			var regionW:int = roomW / columns;
			var regionH:int = roomH / rows;
			
			var regionX:int = 0;
			var regionY:int = regionH * (rows-1);
			
			for (var i = 0; i < rows; i++)
			{
				regionX = 0;
				for (var j = 0; j < columns; j++)
				{
					var region = new Region();
					region.setRectangle(regionX, regionY, regionW, regionH);
					//trace("row: "+i+" col: "+j+" x: "+regionX+" y: "+regionY+" w: "+regionW+" h: "+regionH);//TMP
					regions.push(region);

					regionX += regionW;				
				}

				regionY -= regionH;				
			}
		}
		
		public function count() {
			return regions.length;
		}
		
		public function getItems(regionId:int) {
			if (regionId==0 || regionId>count()) throw new Error("regionId should be in range 1 - "+count());
			return regions[regionId-1].getItems();
		}
		
		public function addItem(regionId:int, newItem){
			trace("add "+newItem+" to region "+regionId);
			if (regionId==0 || regionId>count()) throw new Error("regionId should be in range 1 - "+count());
			regions[regionId-1].addItem(newItem);
		}
		
		public function removeItem(regionId:int, oldItem){
			if (regionId==0 || regionId>count()) throw new Error("regionId should be in range 1 - "+count());
			regions[regionId-1].removeItem(oldItem);
		}
		
		public function countItems(regionId:int)
		{
			if (regionId==0 || regionId>count()) throw new Error("regionId should be in range 1 - "+count());
			return regions[regionId-1].countItems();
		}
		
		public function getRegion(regionId:int)
		{
			if (regionId==0 || regionId>count()) throw new Error("regionId should be in range 1 - "+count());
			return regions[regionId-1];
		}
	}
	
}